package com.project.JunM.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.JunM.entity.SCell;
import com.project.JunM.entity.SGather;
import com.project.JunM.entity.SGatherDetails;
import com.project.JunM.service.SCellService;
import com.project.JunM.service.SGatherDetailsService;
import com.project.JunM.service.SGatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sGatherDetails")
public class SGatherDetailsController {

    @Autowired
    private SGatherDetailsService sGatherDetailsService;

    @Autowired
    private SGatherService sGatherService;

    @Autowired
    private SCellService sCellService;

    @PostMapping("/querySGatherDetailsAll.action")
    public List<SGatherDetails> querySGatherDetailsAll(Integer parentId){
        return sGatherDetailsService.querySGatherDetailsAll(parentId);
    }

   /* @PostMapping("/querySGatherDetailsAll1")
    public List<SGatherDetails> querySGatherDetailsAll1(Integer parentId){
        return sGatherDetailsService.querySGatherDetailsAll(parentId);
    }*/

    @PostMapping("/querySGatherDetailsCheckInAll.action")
    public List<SGatherDetails> querySGatherDetailsCheckInAll(Integer parentId){
        return sGatherDetailsService.querySGatherDetailsCheckInAll(parentId);
    }

    //根据parentId
    @PostMapping("/updateSGatherDetailsByParentId")
    public void updateSGatherDetailsByParentId(Integer parentId,String sGatherDetailsJson){
        System.out.println(parentId);
        SGatherDetails[] sGatherDetails = JSONObject.parseObject(sGatherDetailsJson, SGatherDetails[].class);
        for(int i=0;i<sGatherDetails.length;i++){
            SGatherDetails sGatherDetail = sGatherDetails[i];
            sGatherDetail.setParentId(parentId);
            //标识符，表示已登记
            sGatherDetail.setGatherTag("K002-1");
            System.out.println(sGatherDetail);
            sGatherDetailsService.updateSGatherDetailsByProductId(sGatherDetail);
        }
        //更改  库存标志（STORE_TAG） K002-1: 已登记
        SGather sGather = new SGather();
        sGather.setId(parentId);
        sGather.setStoreTag("K002-1");
        sGatherService.updateByPrimaryKeySelective(sGather);
    }

    //根据productId
    @PostMapping("/updateSGatherDetailsByProductId")
    public void updateSGatherDetailsByProductId(Integer gatheredAmount,String productId,String registerTime,Integer parentId){
        SGatherDetails sGatherDetails1 = new SGatherDetails();
        sGatherDetails1.setParentId(parentId);
        sGatherDetails1.setProductId(productId);
        //通过产品编号查询出来，然后修改
        SGatherDetails sGatherDetails = sGatherDetailsService.selectByProductId(sGatherDetails1);
        //获取已入库存的数量
        Integer gatheredAmount1 = sGatherDetails.getGatheredAmount();
        gatheredAmount1 = gatheredAmount1 == null ? 0 : gatheredAmount1;
        //已入库存的数量+现在要入库的数量
        sGatherDetails.setGatheredAmount(gatheredAmount1+gatheredAmount);
        //标识符，表示已调度
        sGatherDetails.setGatherTag("K002-2");
        //执行修改
        sGatherDetailsService.updateSGatherDetailsByProductId(sGatherDetails);
        //查询S_Cell 以 产品编号为查询条件
        SCell sCell = sCellService.querySCellByProductById(productId);
        //当前存储量
        Integer amount = sCell.getAmount();
        amount = amount==null ? 0 : amount;
        amount = amount + gatheredAmount;
        sCell.setAmount(amount);
        //最大存储量
        Integer maxCapacityAmount = sCell.getMaxCapacityAmount();
        maxCapacityAmount = maxCapacityAmount-gatheredAmount;
        sCell.setMaxCapacityAmount(maxCapacityAmount);
        //修改 当前存储量 最大存储量
        sCellService.updateSCellByProductId(sCell,1);

        //查询所有sGatherDetails表中的数据，判断是否全部为已调度
        List<SGatherDetails> detailsList = sGatherDetailsService.querySGatherDetails(parentId);
        boolean flag=true;
        for(int i=0;i<detailsList.size();i++){
            System.out.println(detailsList.get(i));
            if(detailsList.get(i).getGatherTag()==""
                    ||detailsList.get(i).getGatherTag()==null){
                flag = false;
            }else if(detailsList.get(i).getGatherTag()=="K002_2"){

            }
        }
        if(flag==true){
            Integer parentId1 = sGatherDetails.getParentId();
            SGather sGather = new SGather();
            sGather.setId(parentId1);
            sGather.setStoreTag("K002-2");
            sGather.setRegisterTime(registerTime);
            sGatherService.updateByPrimaryKeySelective(sGather);
        }


    }


    //调度查询 按照 父级ID 和 产品编号 作为查询 条件
    @PostMapping("/querySGatherDetailsByParentIdByProductId.action")
    public SGatherDetails querySGatherDetailsByParentIdByProductId(Integer parentId,String productId){

        SGatherDetails sGatherDetails = new SGatherDetails();
        sGatherDetails.setParentId(parentId);
        sGatherDetails.setProductId(productId);

        System.out.println(sGatherDetailsService.querySGatherDetailsByParentIdByProductId(sGatherDetails));

        return sGatherDetailsService.querySGatherDetailsByParentIdByProductId(sGatherDetails);


    }

}
